#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
站点设置标签页
"""

from PySide6.QtWidgets import (
    QWidget, QVBoxLayout, QFormLayout, QLineEdit,
    QPushButton, QLabel, QCheckBox
)

from src.ui.base_window import BaseWindow


class SiteSettingsTab(BaseWindow):
    """站点设置标签页"""
    
    def __init__(self, config, client):
        super().__init__()
        self.config = config
        self.client = client
        self.base_url_edit = None
        self.test_account_checkbox = None
        self.username_edit = None
        self.password_edit = None
        
    def build_ui(self) -> QWidget:
        """构建UI界面"""
        widget = QWidget()
        layout = QVBoxLayout(widget)
        
        # 站点地址设置
        site_group = self._create_site_settings_group()
        layout.addWidget(site_group)
        
        # 测试账号设置
        test_group = self._create_test_account_group()
        layout.addWidget(test_group)
        
        # 结果输出区域
        layout.addWidget(QLabel("结果输出："))
        self.result_view = self.create_result_view()
        layout.addWidget(self.result_view)
        
        layout.addStretch()
        return widget
        
    def _create_site_settings_group(self) -> QWidget:
        """创建站点设置组"""
        group = QWidget()
        group.setStyleSheet("""
            QWidget {
                border: 1px solid #ccc;
                border-radius: 5px;
                padding: 10px;
                margin: 5px;
            }
        """)
        
        layout = QFormLayout(group)
        
        # 站点地址输入
        self.base_url_edit = QLineEdit()
        self.base_url_edit.setPlaceholderText("https://example.com")
        self.base_url_edit.setMinimumWidth(600)  # 增加文本框宽度
        self.base_url_edit.setStyleSheet("""
            QLineEdit {
                padding: 8px;
                font-size: 14px;
                border: 2px solid #ccc;
                border-radius: 4px;
            }
            QLineEdit:focus {
                border-color: #4CAF50;
            }
        """)
        current_url = self.config.get_base_url()
        if current_url:
            self.base_url_edit.setText(current_url)
            
        layout.addRow("WordPress 站点地址：", self.base_url_edit)
        
        # 保存按钮
        save_btn = QPushButton("保存站点地址")
        save_btn.clicked.connect(self.on_save_base_url)
        save_btn.setStyleSheet("""
            QPushButton {
                background-color: #4CAF50;
                color: white;
                padding: 5px 15px;
                border: none;
                border-radius: 3px;
            }
            QPushButton:hover {
                background-color: #45a049;
            }
        """)
        layout.addRow(save_btn)
        
        return group
        
    def _create_test_account_group(self) -> QWidget:
        """创建测试账号组"""
        group = QWidget()
        group.setStyleSheet("""
            QWidget {
                border: 1px solid #ccc;
                border-radius: 5px;
                padding: 10px;
                margin: 5px;
            }
        """)
        
        layout = QFormLayout(group)
        
        # 测试账号开关
        self.test_account_checkbox = QCheckBox("使用测试账号")
        self.test_account_checkbox.stateChanged.connect(self.on_toggle_test_account)
        layout.addRow(self.test_account_checkbox)
        
        # 用户名输入
        self.username_edit = QLineEdit()
        self.username_edit.setPlaceholderText("用户名")
        layout.addRow("用户名：", self.username_edit)
        
        # 密码输入
        self.password_edit = QLineEdit()
        self.password_edit.setEchoMode(QLineEdit.EchoMode.Password)
        self.password_edit.setPlaceholderText("密码")
        layout.addRow("密码：", self.password_edit)
        
        return group
        
    def on_save_base_url(self):
        """保存站点地址"""
        url = self.base_url_edit.text().strip()
        if not url.startswith("http"):
            self.show_warning("无效地址", "请填写以 http(s) 开头的站点地址")
            return
            
        self.config.set_base_url(url)
        self.client.set_base_url(url)
        self.show_message("已保存", "站点地址已保存")
        self.print_result("站点设置", {"status": "success", "url": url})
        
    def on_toggle_test_account(self, state: int):
        """切换测试账号开关"""
        if state:  # 选中状态
            test_account = self.config.get_test_account()
            self.username_edit.setText(test_account["username"])
            self.password_edit.setText(test_account["password"])
            self.print_result("测试账号", {"status": "loaded", "account": test_account})
        else:  # 未选中状态
            self.username_edit.clear()
            self.password_edit.clear()
            self.print_result("测试账号", {"status": "cleared"})